﻿using Microsoft.EntityFrameworkCore;
using YDT_UserService.Domain.Entities;
using YDT_UserService.Domain.Enums;
using YDT_UserService.Domain.Interfaces;
using YDT_UserService.Infrastructure.Data;

namespace YDT_UserService.Infrastructure.Repositorys
{
    /// <summary>
    /// 用户仓储实现 - 实现用户数据访问
    /// </summary>
    public class UserRepository : IUserRepository
    {
        private readonly UserServiceDbContext _context;

        /// <summary>
        /// 构造函数
        /// </summary>
        public UserRepository(UserServiceDbContext context)
        {
            _context = context;
        }

        /// <summary>
        /// 根据ID获取用户
        /// </summary>
        public async Task<User?> GetByIdAsync(Guid id)
        {
            return await _context.Users
                .FirstOrDefaultAsync(u => u.Id == id);
        }

        /// <summary>
        /// 根据用户名获取用户
        /// </summary>
        public async Task<User?> GetByUserNameAsync(string userName)
        {
            return await _context.Users
                .FirstOrDefaultAsync(u => u.UserName == userName);
        }

        /// <summary>
        /// 根据手机号获取用户
        /// </summary>
        public async Task<User?> GetByPhoneAsync(string phone)
        {
            return await _context.Users
                .FirstOrDefaultAsync(u => u.UserPhone == phone);
        }

        /// <summary>
        /// 获取所有用户
        /// </summary>
        public async Task<IEnumerable<User>> GetAllAsync()
        {
            return await _context.Users
                .OrderBy(u => u.CreateTime) // 按创建时间排序
                .AsNoTracking() // 只读查询，不跟踪变更
                .ToListAsync();
        }

        /// <summary>
        /// 根据状态获取用户
        /// </summary>
        public async Task<IEnumerable<User>> GetByStatusAsync(UserStatus status)
        {
            return await _context.Users
                .Where(u => u.Status == status)
                .OrderBy(u => u.CreateTime)
                .AsNoTracking() // 只读查询，不跟踪变更
                .ToListAsync();
        }

        /// <summary>
        /// 分页获取用户
        /// </summary>
        public async Task<(IEnumerable<User> Users, int TotalCount)> GetPagedAsync(int pageIndex, int pageSize)
        {
            var query = _context.Users
                .OrderBy(u => u.CreateTime);

            var totalCount = await query.CountAsync();
            var users = await query
                .Skip((pageIndex - 1) * pageSize)
                .Take(pageSize)
                .AsNoTracking() // 只读查询，不跟踪变更
                .ToListAsync();

            return (users, totalCount);
        }

        /// <summary>
        /// 添加用户
        /// </summary>
        public async Task AddAsync(User user)
        {
            await _context.Users.AddAsync(user);
            await _context.SaveChangesAsync();
        }

        /// <summary>
        /// 更新用户
        /// </summary>
        public async Task UpdateAsync(User user)
        {
            _context.Users.Update(user);
            await _context.SaveChangesAsync();
        }

        /// <summary>
        /// 删除用户
        /// </summary>
        public async Task DeleteAsync(Guid id)
        {
            var user = await _context.Users.FindAsync(id);
            if (user != null)
            {
                _context.Users.Remove(user);
                await _context.SaveChangesAsync();
            }
        }

        /// <summary>
        /// 检查用户是否存在
        /// </summary>
        public async Task<bool> ExistsAsync(Guid id)
        {
            return await _context.Users.AnyAsync(u => u.Id == id);
        }

        /// <summary>
        /// 检查用户名是否存在
        /// </summary>
        public async Task<bool> UserNameExistsAsync(string userName)
        {
            return await _context.Users.AnyAsync(u => u.UserName == userName);
        }

        /// <summary>
        /// 检查手机号是否存在
        /// </summary>
        public async Task<bool> PhoneExistsAsync(string phone)
        {
            return await _context.Users.AnyAsync(u => u.UserPhone == phone);
        }
    }
}
